using System;
using System.Drawing;
using System.Reflection;

namespace MIMS.WRS.Business
{
	/// <summary>
	/// Summary description for ImageService.
	/// </summary>
	public class ImageService
	{
		public static byte[] GenerateSquareThumbnail(Image img, int size) 
		{
			// generate thumbnail image on the fly
			int thumbWidth = 0;
			int thumbHeight = 0;

			if (img.Width>img.Height) 
			{
				thumbWidth = Math.Min(size, img.Width);
				thumbHeight = Convert.ToInt32((float)thumbWidth * ((float)img.Height / (float)img.Width));
			}
			else 
			{
				thumbHeight = Math.Min(size, img.Height);
				thumbWidth = Convert.ToInt32((float)thumbHeight * ((float)img.Width / (float)img.Height));
			}
					
			System.Drawing.Image.GetThumbnailImageAbort myCallBack = 
				new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);					

			System.Drawing.Image imgThumbnail = img.GetThumbnailImage(thumbWidth, thumbHeight, myCallBack, IntPtr.Zero);

			System.IO.MemoryStream memStream = new System.IO.MemoryStream();
			imgThumbnail.Save(memStream, img.RawFormat);
					
			byte[] thumbnailData = memStream.ToArray();
					
			imgThumbnail.Dispose();

			return thumbnailData;
		}

		public static bool ThumbnailCallback()
		{
			return false;
		}
	}
}
